import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;

/**
 * Part of the wizard-mode steps. Prompts the user to select
 * what type of variable representation they would like in the
 * generated report.
 * 
 * @author James Stanier
 */
public class VariableGUI implements ActionListener {
	
	KeywordGUI wiz3;
	
	String outputFileName;
	String outputDir;
	String paperType;
	String variableOutput;
	int fontSize;
	boolean twoSidedPrinting;
	boolean inDepthSection;
	boolean keywordIndex;
	
	JFrame window;
	JFrame prev;
	JButton nextButton;
	JButton backButton;
	JComboBox variableSelect;
	
	ArrayList<File> inputFiles;
	
	/**
	 * Constructor.
	 * @param g The previous window, KeywordGUI.
	 */
	public VariableGUI(KeywordGUI g) {
		wiz3 = g;
		inputFiles = wiz3.getInputFiles();
		prev = wiz3.getWindow();
		outputFileName = wiz3.getOutputFileName();
		outputDir = wiz3.getOutputDir();
		paperType = wiz3.getPaperType();
		fontSize = wiz3.getFontSize();
		twoSidedPrinting = wiz3.isTwoSidedPrinting();
		inDepthSection = wiz3.isInDepthSection();
		keywordIndex = wiz3.isKeywordIndex();
		variableOutput = "none";
		variableWindow();
	}
	
	/**
	 * Configures and draws the screen.
	 */
	public void variableWindow() {
		window = new JFrame("JavaLiN - Wizard mode");
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setSize(350,500);
		window.setLocationRelativeTo(null);
		window.toFront();
		window.setResizable(false);
		
		// Introductory passage and logo
		JPanel top = new JPanel();
		top.setLayout(new BorderLayout());
		top.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		
		// Logo
		JLabel logo = new JLabel();
		logo.setIcon(new ImageIcon("logo.png"));
		logo.setHorizontalAlignment(JLabel.CENTER);
		top.add(logo, BorderLayout.NORTH);
		
		// Heading information
		JPanel progress = new JPanel();
		JPanel header = new JPanel();
		progress.setLayout(new BorderLayout());
		header.setLayout(new BorderLayout());
		JLabel backwards = new JLabel("<HTML><font size=2>&#171; Keyword index</HTML>");
		progress.add(backwards, BorderLayout.WEST);
		JLabel forwards = new JLabel("<HTML><font size=2> Code index &#187;</HTML>");
		progress.add(forwards, BorderLayout.EAST);
		JLabel heading = new JLabel("<HTML><b><font size=5>4. Variable output</b></HTML>");
		heading.setHorizontalAlignment(JLabel.CENTER);
		heading.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		header.add(heading, BorderLayout.CENTER);
		JLabel description = new JLabel("<HTML><font size=3>Would you like an index of declared variables " +
				"and their type included in your report? If you do, then they can be presented in a grid " +
				"or an index format. Please choose from the drop-down menu below.</HTML>");
		description.setBorder(BorderFactory.createEmptyBorder(20,10,10,10));
		header.add(description, BorderLayout.SOUTH);
		top.add(progress, BorderLayout.CENTER);
		top.add(header, BorderLayout.SOUTH);
		
		// Combo box
		JPanel comboBoxPanel = new JPanel();
		comboBoxPanel.setLayout(new GridLayout(1,1));
		comboBoxPanel.setBorder(BorderFactory.createEmptyBorder(10,20,20,10));
		
		JLabel variableLabel = new JLabel("<HTML>Variable display</HTML>");
		comboBoxPanel.add(variableLabel);
		String[] variableTypes = {"None","Grid","Index"};
		variableSelect = new JComboBox(variableTypes);
		variableSelect.addActionListener(this);
		variableSelect.setToolTipText("Generate an index or grid of declared variables.");
		comboBoxPanel.add(variableSelect);
		
		// Navigation buttons
		JPanel navButtons = new JPanel();
		navButtons.setLayout(new BorderLayout());
		navButtons.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		backButton = new JButton("<< Back");
		backButton.addActionListener(this);
		navButtons.add(backButton, BorderLayout.WEST);
		nextButton = new JButton("Next >>");
		nextButton.addActionListener(this);
		navButtons.add(nextButton, BorderLayout.EAST);
		
		window.add("North",top);
		window.add("Center",comboBoxPanel);
		window.add("South",navButtons);
		window.validate();
		window.setVisible(true);
		
	}
	
	/**
	 * Actions to be taken upon component interaction.
	 */
	public void actionPerformed(ActionEvent e) {
		Object x = e.getSource();
		if(x.equals(variableSelect)) {
			String temp = (String)variableSelect.getSelectedItem();
			if(temp.equals("None")) {
				variableOutput = "none";
			}
			else if(temp.equals("Index")) {
				variableOutput = "index";
			}
			else if(temp.equals("Grid")) {
				variableOutput = "grid";
			}
		}
		else if(x.equals(backButton)) {
			clearSettings();
			window.setVisible(false);
			prev.setVisible(true);
		}
		else if(x.equals(nextButton)) {
			window.setVisible(false);
			CodeIndexGUI cgui = new CodeIndexGUI(this);
		}
	}
	
	/**
	 * Clear the settings relevant to this screen.
	 */
	public void clearSettings() {
		variableOutput = "none";
	}
	
	/**
	 * Get the current font size.
	 * @return The font size as an integer.
	 */
	public int getFontSize() {
		return fontSize;
	}
	
	/**
	 * Get the current paper type.
	 * @return The paper type as a String.
	 */
	public String getPaperType() {
		return paperType;
	}
	
	/**
	 * Get current two-sided printing status.
	 * @return Whether two-sided printing is enabled.
	 */
	public boolean isTwoSidedPrinting() {
		return twoSidedPrinting;
	}
	
	/**
	 * Get the current window.
	 * @return The current JFrame window.
	 */
	public JFrame getWindow() {
		return window;
	}
	
	/**
	 * Get the current output directory.
	 * @return The output directory as a String.
	 */
	public String getOutputDir() {
		return outputDir;
	}
	
	/**
	 * Get the output file name.
	 * @return The output file name as a String.
	 */
	public String getOutputFileName() {
		return outputFileName;
	}
	
	/**
	 * Get whether an in-depth section has been requested.
	 * @return Whether there will be an in-depth section as a boolean.
	 */
	public boolean isInDepthSection() {
		return inDepthSection;
	}
	
	/**
	 * Get whether a keyword index has been requested.
	 * @return Whether there will be a keyword index as a boolean.
	 */
	public boolean isKeywordIndex() {
		return keywordIndex;
	}
	
	/**
	 * Get the type of variable output requested.
	 * @return Variable output status as a String.
	 */
	public String getVariableOutput() {
		return variableOutput;
	}

	/**
	 * Get the input source file references.
	 * @return Input source files as File objects.
	 */
	public ArrayList<File> getInputFiles() {
		return inputFiles;
	}
}
